package org.zky.modules.merchant.service.impl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.merchant.domain.ZkyCampusTab;
import org.zky.modules.merchant.repository.ZkyCampusTabRepository;
import org.zky.modules.merchant.service.ZkyCampusTabService;
import org.zky.modules.merchant.service.dto.ZkyCampusTabDto;
import org.zky.modules.merchant.service.dto.ZkyCampusTabQueryCriteria;
import org.zky.modules.merchant.service.mapper.ZkyCampusTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyCampusTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyCampusTabServiceImpl implements ZkyCampusTabService {

    private final ZkyCampusTabRepository zkyCampusTabRepository;

    private final ZkyCampusTabMapper zkyCampusTabMapper;

    public ZkyCampusTabServiceImpl(ZkyCampusTabRepository zkyCampusTabRepository, ZkyCampusTabMapper zkyCampusTabMapper) {
        this.zkyCampusTabRepository = zkyCampusTabRepository;
        this.zkyCampusTabMapper = zkyCampusTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyCampusTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyCampusTab> page = zkyCampusTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyCampusTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyCampusTabDto> queryAll(ZkyCampusTabQueryCriteria criteria){
        return zkyCampusTabMapper.toDto(zkyCampusTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyCampusTabDto findById(Long campusId) {
        ZkyCampusTab zkyCampusTab = zkyCampusTabRepository.findById(campusId).orElseGet(ZkyCampusTab::new);
        ValidationUtil.isNull(zkyCampusTab.getCampusId(),"ZkyCampusTab","campusId",campusId);
        return zkyCampusTabMapper.toDto(zkyCampusTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyCampusTabDto create(ZkyCampusTab resources) {
        return zkyCampusTabMapper.toDto(zkyCampusTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyCampusTab resources) {
        ZkyCampusTab zkyCampusTab = zkyCampusTabRepository.findById(resources.getCampusId()).orElseGet(ZkyCampusTab::new);
        ValidationUtil.isNull( zkyCampusTab.getCampusId(),"ZkyCampusTab","id",resources.getCampusId());
        zkyCampusTab.copy(resources);
        zkyCampusTabRepository.save(zkyCampusTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long campusId : ids) {
            zkyCampusTabRepository.deleteById(campusId);
        }
    }

    @Override
    public void download(List<ZkyCampusTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyCampusTabDto zkyCampusTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" campusName",  zkyCampusTab.getCampusName());
            map.put("logos", zkyCampusTab.getLogo());
            map.put("campus contact", zkyCampusTab.getContact());
            map.put(" mainPersonName",  zkyCampusTab.getMainPersonName());
            map.put(" mainPersonMobileNo",  zkyCampusTab.getMainPersonMobileNo());
            map.put(" province",  zkyCampusTab.getProvince());
            map.put(" city",  zkyCampusTab.getCity());
            map.put(" region",  zkyCampusTab.getRegion());
            map.put(" address",  zkyCampusTab.getAddress());
            map.put(" mechanismId",  zkyCampusTab.getMechanismId());
            map.put("1:active 2:inactive", zkyCampusTab.getStatus());
            map.put("delete time", zkyCampusTab.getDeleteAt());
            map.put(" createTime",  zkyCampusTab.getCreateTime());
            map.put(" updateTime",  zkyCampusTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}